<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use DB;
class NewController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = DB::table("news")->get();
        return view('new.index',['data' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    //浏览
    public function create()
    {
        return view("new.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    //新闻添加
    public function store(Request $request)
    {
        //insertGetId 返回的是自增ID
        //$res = DB::table('new')->insert(['title' => $title,"aid"=>$aid]);
        //获取所有的表单信息
        //$res = $request->all();
        //$res = $_POST;
        //获取单挑数据

        //$res = $request ->input("title";)
        //$res = $request->path();//访问的路由
        //获取指定参数数值
        //$data = $request->only("title");
        //获取指定外部参数数值
        //$data = $request->except("title");
        
        //$res = $request->url();
        dd($data);
        $time = $request->inputtime = time();
        $res = DB::table('news')->insert(['title' => $request->title,'catid' => $request->catid,'inputtime' => $time]);
        if($res > 0){
            echo "添加成功";
        }else{
            echo "添加失败";
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    public function show($id)
    {
        return "浏览单个页面";
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        //
        $id = $request->route("new");
       $data = DB::table("news")->where("id",$id)->get();
       return view('new.edit', ['data' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $inputtime = $request->inputtime = time();
        $id = $request->id;
        $catid = $request->catid;
        $title = $request->title;
        $res = DB::table('news')->where('id', $id)->update(['catid' => $catid,'title' => $title,'inputtime' => $inputtime]);
       if($res){
            echo "修改成功";
        }else{
            echo "修改失败";
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return "执行删除页面";
    }

    //删除操作
    public function delete(Request $request)
    {
       $id = $request->id;
       $res = DB::table('news')->where('id',$id)->delete();
       if($res){
            echo "删除成功";
       }else{
            echo "删除失败";
       }
    }
}
